package com.zhou.job;

import com.alibaba.fastjson.JSON;
import com.zhou.pojo.Register;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.client.RestTemplate;

import java.net.InetAddress;
import java.net.UnknownHostException;

@Configuration      //1.主要用于标记配置类，兼备Component的效果。
public class MyJob {
    private RestTemplate restTemplate = new RestTemplate();

    //3.添加定时任务
    @Scheduled(cron = " 0 0/1 * * * ?")
    private void configureTasks() {
        System.out.println("start  Job：发送心跳");
        try {
            String ip = InetAddress.getLocalHost().getHostAddress();//获取 ip地址
            Register register = new Register();
            register.setIp(ip);
            String url = "http://localhost:10000/server/edit";
            restTemplate.postForEntity(url, register, String.class);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }

    @Scheduled(cron = "0/30 * * * * ?")
    private void configureTasks2() {
        System.out.println("start  Job：拉取服务");
        String url = "http://localhost:10000/server/findAll";
        String forObject = restTemplate.getForObject(url, String.class);
        System.out.println(forObject);
    }
}
